package com.example.zzy.zzytest.data.structure.queue;

import java.util.Scanner;

public class CircleQueueDemo {
    public static void main(String[] args) {
        CircleQueue queue = new CircleQueue(4);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean isLoop = true;
        while (isLoop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    queue.showQueue();
                    break;
                case 'e':
                    scanner.close();
                    isLoop = false;
                    break;
                case 'a':
                    System.out.println("输入一个数字");
                    int i = scanner.nextInt();
                    queue.addQueue(i);
                    break;
                case 'g':
                    System.out.println("取出数据>>>" + queue.getQueue());
                    System.out.println("队列数据：");
                    queue.showQueue();
                    break;
                case 'h':
                    System.out.println("头部数据>>>" + queue.headQueue());
                    System.out.println("队列数据：");
                    queue.showQueue();
                    break;
                default:
                    break;
            }
        }
    }

    static class CircleQueue {
        private int maxSize;
        //队首位置
        private int front;
        //队尾后面的一个位置
        private int rear;
        private int[] arr;

        public CircleQueue(int maxSize) {
            this.maxSize = maxSize;
            this.front = 0;
            this.rear = 0;
            this.arr = new int[maxSize];
        }

        // 判断队列是否满
        public boolean isFull() {
            return (rear + 1) % maxSize == front;
        }

        // 判断队列是否为空
        public boolean isEmpty() {
            return rear == front;
        }

        // 添加数据到队列
        public void addQueue(int n) {
            if (isFull()) {
                System.out.println("队列已满");
                return;
            }
            arr[rear] = n;
            rear = (rear + 1) % maxSize;

        }

        // 获取队列的数据, 出队列
        public int getQueue() {
            if (isEmpty()) {
                throw new RuntimeException("队列为空");
            }
            int temp = arr[front];
            front = (front + 1) % maxSize;
            return temp;
        }

        // 显示队列的所有数据
        public void showQueue() {
            if (isEmpty()) {
                return;
            }
            for (int i = front; i < front + queueSize(); i++) {
                System.out.print(arr[i % maxSize] + "\t");
            }
        }

        public int queueSize() {
            return (rear - front + maxSize) % maxSize;
        }

        // 显示队列的头数据， 注意不是取出数据
        public int headQueue() {
            if (isEmpty()) {
                throw new RuntimeException("队列为空");
            }
            return arr[front];
        }
    }
}
